package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class FinestraStorico extends JFrame implements KeyListener {
	private String doc;
	private MyResultSet rs;
	private JPanel pannelloPrincipale, pannelloRicerca, pannelloTabella;
	private MyDefaultTableModel modelClienti;
	private JTable tabella;
	private JScrollPane scroll;
	private JLabel cerca;
	private JTextField cercaT;
	private GraficaHotel g;

	public FinestraStorico(String documento,GraficaHotel g) {
		doc = documento;
		this.g=g;
		Image immagineIc = Toolkit.getDefaultToolkit().getImage(
				"Risorse/storico1.png");
		setTitle("Storico");
		setIconImage(immagineIc);
		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocation(400,200);
		this.setSize(500, 500);
		pannelloPrincipale = new JPanel(new BorderLayout());
		pannelloRicerca = new JPanel();
		pannelloTabella = new JPanel();
		add(pannelloPrincipale);
		cerca = new JLabel("Cerca per data di arrivo:");
		cercaT = new JTextField(20);
		cercaT.addKeyListener(this);
		pannelloRicerca.add(cerca);
		pannelloRicerca.add(cercaT);
		pannelloPrincipale.add(pannelloRicerca, BorderLayout.NORTH);
		ottieniInformazioni();
		scroll = new JScrollPane(tabella);
		pannelloTabella.add(scroll);
		pannelloPrincipale.add(pannelloTabella, BorderLayout.CENTER);
		this.setVisible(true);
	}

	private void ottieniInformazioni() {
		modelClienti = new MyDefaultTableModel();
		rs = g.database.querySelect("SELECT numerodocumento,numcamera,DATE_FORMAT(dataarrivo,'%d/%m/%Y'),DATE_FORMAT(datapartenza,'%d/%m/%Y'),prezzo FROM "+g.username+".STORICO WHERE numerodocumento='"
						+ doc + "'");
		int columnCount = 5;
		modelClienti.addColumn("Numero Camera");
		modelClienti.addColumn("Data di Arrivo");
		modelClienti.addColumn("Data di Partenza");
		modelClienti.addColumn("Prezzo");
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(3);
			for (int i = 2; i <= columnCount; i++) {
				row.add(rs.getString(i));
			}
			modelClienti.addRow(row);
		}
		tabella = new JTable(modelClienti);
		tabella.setGridColor(Color.LIGHT_GRAY);
		tabella.setVisible(true);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getSource() == cercaT) {
			String query = "SELECT numerodocumento,numcamera,DATE_FORMAT(dataarrivo,'%d/%m/%Y'),DATE_FORMAT(datapartenza,'%d/%m/%Y'),prezzo FROM "+g.username+".storico  WHERE DATE_FORMAT(dataarrivo,'%d/%m/%Y') like '"
					+ cercaT.getText() + "%' && numerodocumento='"+doc+"'";
			try {
				azzeraTabellaClienti();
				ricercaInTabellaClienti(query);
			} catch (SQLException e1) {
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		//

	}

	@Override
	public void keyTyped(KeyEvent e) {
		//Auto-generated method stub

	}

	public void azzeraTabellaClienti() throws SQLException {
		modelClienti.setRowCount(0);
	}// azzeraTabellaClienti
	
	public void ricercaInTabellaClienti(String query) throws SQLException, ClassNotFoundException {
		rs = g.database.querySelect(query);
		int columnCount = 5;
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(columnCount);
			for (int i = 2; i <= columnCount; i++) {
					row.add(rs.getString(i));
			}
			modelClienti.addRow(row);
		}
		tabella.setModel(modelClienti);
	}// ricercaTabellaClienti

}

class MyDefaultTableModel extends DefaultTableModel {
	private static final long serialVersionUID = 1L;

	public boolean isCellEditable(int row, int column) {
		return false;
	}

}
